package com.extweb4j.core.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.extweb4j.core.enums.RowStatus;
import com.extweb4j.core.kit.ExtKit;
import com.extweb4j.core.kit.LoginKit;
import com.jfinal.plugin.activerecord.Page;


/**
 * 定时任务
 * @author Administrator
 *
 */
public class ExtJob extends CoreModel<ExtJob>{

	private static final long serialVersionUID = 1L;

	public static ExtJob dao = new ExtJob();
	
	public Page<ExtJob> paginateByKeywaord(int page, int limit,String keywords,String uid){
		String select  =  "SELECT m.msg_title,m.msg_content,j.id,j.cron,j.excute_time,j.status,j.uid,j.create_time";
		StringBuffer form = new StringBuffer(" FROM ext_job j  ");
		
		form.append(" LEFT JOIN  ext_msg m ON j.msg_id = m.id");
		List<Object> paras = new ArrayList<Object>();
		
		form.append(" WHERE j.status != ? ");
		paras.add(RowStatus.删除.getState());
		//判断是否超级管理员，超级管理员可以查看所有信息，其他角色只能查看自己的
		if(LoginKit.isAdmin(uid)){
			form.append(" and j.uid=?");
			paras.add(uid);
		}
		//模糊查询
		if(StringUtils.isNotBlank(keywords)){
			keywords = ExtKit.sqlFilterValidate(keywords);
			form.append(" AND (m.msg_title LIKE '%"+keywords+"%' OR m.msg_content LIKE '%"+keywords+"%' ");
			form.append(" ORDER BY j.status ASC,j.create_time DESC");
		}
		return super.paginate(page, limit, select, form.toString(), paras.toArray());
	}

}
